﻿using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using CoachingOrganizer.WinPhone.UITools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.WinPhone.ViewModels.Tools.MoneyCalculation
{
    public class StableMoneyFlowItemViewModel : ViewModelBase
    {
        private readonly StableMoneyFlowItemModel _model;

        public string Name
        {
            get { return _model.Name; }
            set { _model.Name = value; }
        }

        public decimal Amount
        {
            get { return _model.Amount; }
            set { _model.Amount = value; }
        }

        public int PeriodIndex
        {
            get { return PeriodToIndex(_model.Period); }
            set { SetPeriodFromIndex(value); }
        }

        public DateTime Start
        {
            get { return _model.Start; }
            set { _model.Start = value; }
        }

        public DateTime Finish
        {
            get { return _model.Finish; }
            set { _model.Finish = value; }
        }

        public StableMoneyFlowItemViewModel(StableMoneyFlowItemModel model)
        {
            _model = model;
        }

        private int PeriodToIndex(TimeSpan timeSpan)
        {
            if (timeSpan == TimeSpan.FromDays(1.0))
                return 0;
            else if (timeSpan == TimeSpan.FromDays(7.0))
                return 1;
            else if (timeSpan == TimeSpan.FromDays(StableMoneyFlowItemModel.AsDaysInMonth))
                return 2;
            else if (timeSpan == TimeSpan.FromDays(StableMoneyFlowItemModel.AsDaysInYear))
                return 3;
            else
                throw new NotImplementedException();
        }

        private void SetPeriodFromIndex(int index)
        {
            switch (index)
            {
                case 0:
                    _model.Period = TimeSpan.FromDays(1.0);
                    break;
                case 1:
                    _model.Period = TimeSpan.FromDays(7.0);
                    break;
                case 2:
                    _model.SetMonthPeriod();
                    break;
                case 3:
                    _model.SetYearPeriod();
                    break;
                default:
                    throw new NotImplementedException();
                    break;
            }
        }
    }
}
